//
//  RepairDetailController.m
//  PropertyMananger
//
//  Created by bh on 17/1/16.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "RepairDetailController.h"
#import "RepairDetailCell.h"
#import "DescribeCell.h"
#import "OrderDetailStateCell.h"
#import "UpLoadImgCell.h"

@interface RepairDetailController ()<UIGestureRecognizerDelegate>
@property (nonatomic,copy) NSString * repiarTel;//维修人电话

@property (nonatomic,strong)  PhotoMode * photoModel;//图片模型 RWorkerMode * model

@property (nonatomic,strong) RWorkerMode * workModel;

@end

@implementation RepairDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"公共报修单详情"];
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH -64) isGroup:NO];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self initData];
}
- (void)initData{
    
    self.imageViews = [NSMutableArray array];
    for (PhotoMode * model in self.model.photoModeArray) {
        
        [self.imageViews addObject:model.PhotoName];
    }
//    self.urlsSmall = @[@"http://img1.imgtn.bdimg.com/it/u=321087086,3298231199&fm=206&gp=0.jpg",
//                       @"http://img3.imgtn.bdimg.com/it/u=1183223528,3058066243&fm=206&gp=0.jpg",
//                       @"http://img1.imgtn.bdimg.com/it/u=1825165654,1935296637&fm=206&gp=0.jpg"];
//    self.urlsBig = @[@"http://v1.qzone.cc/pic/201309/29/22/12/5248354649fec935.jpg%21600x600.jpg",
//                     @"http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg",
//                     @"http://img2.3lian.com/img2007/19/33/005.jpg"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.model.photoModeArray.count != 0) {
        
        self.photoModel = self.model.photoModeArray[section];
    }
    
    if ([self.model.ProcessState isEqualToString:@"10"] || [self.model.ProcessState isEqualToString:@"30"]) {
        
        return 11;
        
    }else{
    
    return 12;
    }
    

    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RepairDetailCell * cell = [RepairDetailCell cellWithTableView:tableView];
    
  //  RWorkerMode * model = nil;
    if(self.model.rWorkerModeArray.count != 0){
        
        self.workModel = self.model.rWorkerModeArray[0];
       DLog(@"维修人姓名 = %@",self.workModel.RealName);
      self.repiarTel = self.workModel.Tel;
        
    }
    
    if (indexPath.row == 0) {
        
        self.titleStr = @"订单编号:";
        self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.RepairCode];
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
    }else if (indexPath.row == 1){
        self.titleStr = @"报修类型:";
        if (self.model.CategoryName != nil) {
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.CategoryName];
            
        }else{
            
            self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,@"暂无"];
            
        }
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
        
    }else if (indexPath.row == 2){
        self.titleStr = @"报修人:";
        if (self.model.LinkMan == nil) {
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,@"暂无"];
        }else{
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.LinkMan];
        }
        
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
    }else if (indexPath.row == 3){
        
        self.titleStr = @"报修人电话:";
        if (self.model.LinkTel == nil) {
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,@"暂无"];
        }else{
            
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.LinkTel];
            
            [cell.phoneBtn setImage:IMG(@"ico_dial_telephone") forState:UIControlStateNormal];
            [cell.phoneBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.phoneBtn.tag = 300;
        }
        
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
       
        
    }else if (indexPath.row == 4){
        
        self.titleStr = @"维修人:";
        if (self.workModel.RealName == nil) {
            
            self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,@"暂无"];
            
        }else{
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.workModel.RealName];
        }
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
    }else if (indexPath.row == 5){
        
        self.titleStr = @"维修人电话:";
        if (self.workModel.Tel == nil) {
            self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,@"暂无"];
        }else{
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.workModel.Tel];
            
            [cell.phoneBtn setImage:IMG(@"ico_dial_telephone") forState:UIControlStateNormal];
            [cell.phoneBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.phoneBtn.tag = 310;
        }
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
       
        
    }else if (indexPath.row == 6){
        
        self.titleStr = @"报修时间:";
        if (self.model.ApplyTime == nil) {
            self.pingjieStr = [NSString stringWithFormat:@"%@ %@",self.titleStr,@"暂无"];
        }else{
  
            self.pingjieStr = [NSString stringWithFormat:@"%@  %@",self.titleStr,self.model.ApplyTime];
        }
        
        self.stra = [[NSMutableAttributedString alloc]initWithString:self.pingjieStr];//可随意拼接字符串
        [self.stra addAttributes:@{NSFontAttributeName:BHFont(15),NSForegroundColorAttributeName:BHColor(102, 102, 102)} range:NSMakeRange(0, self.titleStr.length)];
        cell.infoLabel.attributedText = self.stra;
        
    }else if (indexPath.row == 7){
        
        DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
        
        cell.titleLabel.text = @"报修地址";
        if (self.model.RepairAddr != nil) {
            
            cell.infoLabel.text = self.model.RepairAddr;
        }else{
            
            cell.infoLabel.text = @"未填写";
        }
        
        return cell;
        
    }else if (indexPath.row == 8){
        
        DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
        
        cell.titleLabel.text = @"问题描述";
        if (self.model.RepairDesc == nil) {
            
            cell.infoLabel.text = @"未描述";
        }else{
            cell.infoLabel.text = self.model.RepairDesc;
        }
        
        return cell;
        
    }else if (indexPath.row == 9){
        
        if (self.model.photoModeArray.count == 0) {
            
            DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
            
            cell.titleLabel.text = @"上传图片";
            
            cell.infoLabel.text = @"暂无图片";
            
            return cell;
            
        }
        UpLoadImgCell * cell = [UpLoadImgCell cellWithTableView:tableView];
        
        cell.urlsSmall = self.imageViews;
        cell.urlsBig = self.imageViews;
        
        return cell;
    
    }else if (indexPath.row == 10){
        
        if ([self.model.ProcessState isEqualToString:@"10"] || [self.model.ProcessState isEqualToString:@"30"]) {
            
            OrderDetailStateCell * orderDetailCell = [OrderDetailStateCell cellWithTableView:tableView];
            orderDetailCell.processState = self.model.ProcessState;
            DLog(@"详情的处理状态 == %@",self.model.ProcessState);
            return orderDetailCell;
        }else{
        
            DescribeCell * cell = [DescribeCell cellWithTableView:tableView];
            
            cell.titleLabel.text = @"处理结果";
            
            cell.infoLabel.text = self.model.ReplyContent;
            
            return cell;
        }
       
        
    }else if (indexPath.row == 11){
        
        OrderDetailStateCell * orderDetailCell = [OrderDetailStateCell cellWithTableView:tableView];
        orderDetailCell.processState = self.model.ProcessState;
        DLog(@"详情的处理状态 == %@",self.model.ProcessState);
        return orderDetailCell;
    }
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 7) {
        
    return  [DescribeCell cellWorkAddressHeightWiht:self.model];
        
    }else if (indexPath.row == 8){
    
         return  [DescribeCell cellWorkDescQuestionHeightWiht:self.model];
    }
    else if (indexPath.row == 9){
      
        if (self.model.photoModeArray.count == 0) {
            
            return 70;
        }
            return 130;

        
    }else if (indexPath.row == 10){
        
        if ([self.model.ProcessState isEqualToString:@"10"] || [self.model.ProcessState isEqualToString:@"30"]) {
            
            return 90;
        }else{
            
            return [DescribeCell cellWorkResultHeightWiht:self.model];
        }
        
    }else if (indexPath.row == 11){
        
            return 90;
        
    }
    return 50;
    
}
- (void)phoneBtnClick:(UIButton *)button{
    
    DLog(@"点击了 === %ld",button.tag);
    if (button.tag == 300) {
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.LinkTel];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
        
    }else if (button.tag == 310){
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.repiarTel];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];
        
    }
    
}
#pragma mark - 点击浏览图片（点击图片放大）
- (void)imageViewTapped:(UITapGestureRecognizer *)tap{
    
    
    UIImageView *imageView = (UIImageView *)tap.view;
    
    NSLog(@"点击了图片====%ld",imageView.tag);
    DLog(@"%ld",tap.view.tag);
    
}


@end
